<template lang="html">
    <div class="container" v-loading="loading">

        <div class="filter">
            <datePicker startEndTime @onFilter="onFilter" />
            <span>统计时间：{{statisTime}}</span>
        </div>

        <div class="summary">
            <div class="summary_item">
                <div class="summary_title">
                  <img src="../../assets/img/s1.png" alt="">
                  <span>销售业绩</span>
                </div>
                <div class="summary_content">
                    <div class="price">￥{{table.sumPrice}}</div>
                    <div class="growth_rate">
                      <p v-if="table.sumPriceGrowType == 0">持平</p>
                      <p v-if="table.sumPriceGrowType == -1">减少</p>
                      <p v-if="table.sumPriceGrowType == 1">增加</p>
                      <p>
                        <i v-if="table.sumPriceGrowType == 0" class="el-icon-minus"></i>
                        <img v-if="table.sumPriceGrowType == -1" src="../../assets/img/drop.png" alt="">
                        <img v-if="table.sumPriceGrowType == 1" src="../../assets/img/rise.png" alt="">
                        <span>{{table.sumPriceGrow}}%</span>
                      </p>
                    </div>
                </div>
            </div>
            <div class="summary_item">
                <div class="summary_title">
                  <img src="../../assets/img/s2.png" alt="">
                  <span>售出商品（件）</span>
                </div>
                <div class="summary_content">
                    <div class="price">{{table.orderCount}}</div>
                    <div class="growth_rate">
                      <p v-if="table.orderCountGrowType == 0">持平</p>
                      <p v-if="table.orderCountGrowType == -1">减少</p>
                      <p v-if="table.orderCountGrowType == 1">增加</p>
                      <p>
                        <i v-if="table.orderCountGrowType == 0" class="el-icon-minus"></i>
                        <img v-if="table.orderCountGrowType == -1" src="../../assets/img/drop.png" alt="">
                        <img v-if="table.orderCountGrowType == 1" src="../../assets/img/rise.png" alt="">
                        <span>{{table.orderCountGrow}}%</span>
                      </p>
                    </div>
                </div>
            </div>
            <div class="summary_item">
                <div class="summary_title">
                  <img src="../../assets/img/s3.png" alt="">
                  <span>客户数量</span>
                </div>
                <div class="summary_content">
                    <div class="price">{{table.memberCount}}</div>
                    <div class="growth_rate">
                      <p v-if="table.memberCountGrowType == 0">持平</p>
                      <p v-if="table.memberCountGrowType == -1">减少</p>
                      <p v-if="table.memberCountGrowType == 1">增加</p>
                      <p>
                        <i v-if="table.memberCountGrowType == 0" class="el-icon-minus"></i>
                        <img v-if="table.memberCountGrowType == -1" src="../../assets/img/drop.png" alt="">
                        <img v-if="table.memberCountGrowType == 1" src="../../assets/img/rise.png" alt="">
                        <span>{{table.memberCountGrow}}%</span>
                      </p>
                    </div>
                </div>
            </div>
        </div>


        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input class="form_input" v-model="searchText" placeholder="请输入店铺名称"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                  <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables">
          <el-table :data="tableData" :row-class-name="rowStyle" @selection-change="handleSelectionChange" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column prop="branchStoreName" label="店铺名称" align="center"></el-table-column>
                <el-table-column prop="area" label="店铺地区" align="center"></el-table-column>
                <el-table-column prop="address" label="店铺地址" align="center"></el-table-column>
                <el-table-column prop="sumCount" label="客户" align="center"></el-table-column>
                <el-table-column prop="sumPrice" label="业绩" align="center"></el-table-column>
                <el-table-column prop="userName" label="管理员" align="center"></el-table-column>
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="220">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                        <el-button type="warning" size="mini" @click="fDetail(scope.row)">详情</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>

        <el-dialog title="新增分店" v-loading="loading" append-to-body :visible.sync="dialogVisible" width="30%">
            <div class="form">
                <div class="form_item">
                    <span> <i>*</i> 店铺名称</span>
                    <el-input v-model="dialogForm.branchStoreName"></el-input>
                </div>
                <div class="form_item">
                    <span> 管理员账号</span>
                    <el-input v-model="dialogForm.adminUserName"></el-input>
                </div>
                <div class="form_item">
                    <span> <i>*</i> 所在区域</span>
                    <el-input v-model="dialogForm.area"></el-input>
                </div>
                <div class="form_item">
                    <span> <i>*</i> 具体地址</span>
                    <el-input type="textarea" v-model="dialogForm.address"></el-input>
                </div>
            </div>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="confirm">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
export default {
  data(){
    return {
      loading: false,
      searchText: '',
      statisTime: '',
      year: '',
      month: '',
      day: '',
      date: [],
      currentPage: 1,
      totalPage: 0,
      pageSize: 10,
      tableData: [],
      table:{},
      dialogVisible:false,
      dialogTitle: '',
      dialogForm: {},
      currentData: [],
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    },
    queryId: function () {
      return this.$route.query.id
    }
  },
  created(){
    this.selectBranchStorePage();
  },
  methods:{
    selectBranchStorePage(){
      this.loading = true;
      this.$http.selectBranchStorePage({
        id: this.queryId,
        query: this.searchText,
        startTime: (this.date&&this.date[0]) || '',
        endTime: (this.date&&this.date[1]) || '',
        dayTime: this.day,
        monthTime: this.month,
        yearTime: this.year,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      }).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.table = res.data.data;
          this.tableData = res.data.data.pageInfo.rows;
          this.totalPage = res.data.data.pageInfo.total;
          this.pageSize = res.data.data.pageInfo.pageSize;
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    onFilter(e){
      this.statisTime = e.val;
      this.date = [];
      this.year = '';
      this.month = '';
      this.day = '';
      switch (e.key) {
        case 1: this.day = e.val; break;
        case 2: this.month = e.val; break;
        case 3: this.year = e.val; break;
        case 4:
          this.date = [...e.val];
          if(e.val[0]) this.statisTime = `${e.val[0]} - ${e.val[1]}`;
          else this.statisTime = '';
        break;
      }
      this.onSubmit()
    },

    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些账号吗？').then(res=>{
        return this.$http.deleteBranchStores({ids:aId.join(',')});
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectBranchStorePage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    fDelete(e){
      this.$confirm('您确定要删除这个店铺吗？').then(res=>{
         this.$http.deleteBranchStore({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectBranchStorePage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    insertBranchStore(data){
      this.$http.insertBranchStore(data).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.dialogVisible = false;
          this.onSubmit();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    updateBranchStore(data){
      this.$http.updateBranchStore(data).then(res=>{
        this.loading = false;
        if(res.data.code == 0){
          this.$message.success(res.data.message);
          this.dialogVisible = false;
          this.onSubmit();
        }else{
          this.$message.error(res.data.message);
        }
      });
    },
    onAddtion(){
      this.dialogTitle = '新增分店';
      this.dialogForm = {};
      this.dialogForm.mainId = this.queryId;
      this.dialogVisible = true;
    },
    fEdit(e){
      this.dialogTitle = '编辑分店';
      this.dialogForm = {};
      this.dialogForm = {
        id:e.id,
        branchStoreName:e.branchStoreName,
        adminUserName:e.userName,
        area:e.area,
        address:e.address,
      };
      this.dialogVisible = true;
    },
    confirm(){
      if(!this.dialogForm.branchStoreName){
        this.$message.error('请填写店铺名称！！');
        return false;
      }else if(!this.dialogForm.area){
        this.$message.error('请填写区域名称！！');
        return false;
      }else if(!this.dialogForm.address){
        this.$message.error('请填写地址！！');
        return false;
      }
      this.loading = true;
      if(this.dialogTitle == '新增分店'){
        this.insertBranchStore(this.dialogForm);
      }else{
        this.updateBranchStore(this.dialogForm);
      }
    },

    fDetail(e){
      this.$router.push({
        path: '/shop/list/office_detail',
        query: {id: e.id}
      })
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    onSwitch(e){
      this.$http.closeBranchStore({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectBranchStorePage();
    },

  }

}
</script>

<style lang="css" scoped>
i{font-style: normal;color:#FB3F3F}
.el-icon-minus{color: #4196ff}
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.form{
  padding: 20px 0;
}
.form .form_item{
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}
.form .form_item>span:nth-of-type(1){
  font-size: 13px;
  color: #333333;
  margin-right: 10px;
  width: 100px;
}
.form .form_tag{
  margin-right: 10px;
}
.summary{
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
.summary .summary_item{
  width: 32%;
  background-color: #fff;
  box-sizing: border-box;
  padding: 25px 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
}
.summary_title{
  line-height: 32px;
  font-size: 16px;
}
.summary_content{
  display: flex;
  justify-content: space-between;
  padding-top: 20px
}
.summary_content .price{
  font-size: 32px;
}
.summary_content .growth_rate{
  text-align:  center;
  font-size: 14px;
}
.filter{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.filter span{
  font-size: 14px;
  color: #656565
}
</style>
